﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace CloudDcPc.Util
{
    class SendMail
    {
        private string server;
        private int port;
        private string from;
        private string pwd;
        public SendMail()
        {
            this.server = "smtp.163.com";
            this.port = 25;
            this.from = "chinachinesechina2@163.com";
            this.pwd = "822846768";
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="toAddr">收件人人地址</param>
        /// <param name="Cc">抄送地址</param>
        /// <param name="Mcc">密送地址</param>
        /// <param name="from">发送人地址</param>
        /// <param name="content">邮件内容</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="attach">附件内容</param>
        /// <param name="Pwd">发送人邮件密码</param>
        public void SendMailByPlainFormat(string toAddr,string content, string subject, string attach)
        {
            MailMessage mailobj = new MailMessage();
            mailobj.From = new MailAddress(this.from);//发件人
            mailobj.To.Add(toAddr); //收件人
            mailobj.Priority = MailPriority.High;  //发送优先级
            mailobj.Subject = subject; //主题
            mailobj.Body = content;   //内容
            mailobj.IsBodyHtml = true; //内容是否可以为html形式
            mailobj.BodyEncoding = Encoding.Default;
            if (attach != "")
            {
                char[] delim = new char[] { ';' };
                foreach (string substr in attach.Split(delim))
                {
                    Attachment MyAttach = new Attachment(substr);
                    //MailAttachment MyAttach = new MailAttachment(substr);
                    mailobj.Attachments.Add(MyAttach);

                }
            }

            SmtpClient smtp = new SmtpClient();
            smtp.Host = this.server;  //服务器
            smtp.Port = this.port;    //端口        
            smtp.Credentials = new System.Net.NetworkCredential(this.from, this.pwd); //用户名和密码
            smtp.Send(mailobj);
        }
    }
}
